package com.kly.springbootinit.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kly.springbootinit.common.ErrorCode;
import com.kly.springbootinit.exception.ThrowUtils;
import com.kly.springbootinit.mapper.OrderMapper;
import com.kly.springbootinit.model.dto.order.OrderAddRequest;
import com.kly.springbootinit.model.entity.Orders;
import com.kly.springbootinit.model.entity.Vehicle;
import com.kly.springbootinit.model.enums.VehicleStatus;
import com.kly.springbootinit.model.vo.OrderVO;
import com.kly.springbootinit.service.OrderService;
import com.kly.springbootinit.service.VehicleService;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.Duration;
import java.util.Date;
import java.util.Objects;

/**
 * @author admin
 * @description 针对表【order(订单表)】的数据库操作Service实现
 * @createDate 2025-05-21 14:34:04
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Orders>
        implements OrderService {

    @Resource
    @Lazy
    private VehicleService vehicleService;

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public OrderVO createOrder(OrderAddRequest orderAddRequest) {
        Long vehicleId = orderAddRequest.getVehicleId();
        Long userId = orderAddRequest.getUserId();

        Vehicle oldVehicle = vehicleService.getById(vehicleId);
        ThrowUtils.throwIf(oldVehicle == null, ErrorCode.NOT_FOUND_ERROR);
        ThrowUtils.throwIf(!Objects.equals(oldVehicle.getStatus(), VehicleStatus.AVAILBLE.getStatus()), ErrorCode.OPERATION_ERROR);
        // 开始租车
        BigDecimal dailyRate = orderAddRequest.getDailyRate();
        Integer rentDays = orderAddRequest.getRentDays();

        // 创建订单
        Orders orders = new Orders();
        orders.setUserId(userId);
        orders.setVehicleId(vehicleId);
        // 当前日期
        Date start = new Date();
        // 日期加天数
        Date end = Date.from(start.toInstant().plus(Duration.ofDays(rentDays)));
        orders.setStartTime(start);
        orders.setEndTime(end);
        // 计算消费
        BigDecimal dayBigDecimal = new BigDecimal(rentDays);
        BigDecimal totalCost = dailyRate.multiply(dayBigDecimal);
        orders.setTotalCost(totalCost);
        // 保存到数据库
        synchronized (userId.toString().intern()) {
            this.save(orders);
        }

        // 修改车辆状态
        boolean update = vehicleService.lambdaUpdate()
                .eq(Vehicle::getId, vehicleId)
                .eq(Vehicle::getStatus, VehicleStatus.AVAILBLE.getStatus())
                .set(Vehicle::getStatus, VehicleStatus.RENTED.getStatus())
                .update();
        ThrowUtils.throwIf(!update, ErrorCode.PARAMS_ERROR, "车辆状态修改失败");

        OrderVO orderVO = BeanUtil.copyProperties(orders, OrderVO.class);
        return orderVO;
    }
}




